import unittest
import requests

from api.login_api import RegLoginApi
from api.open_account_api import OpenAccountApi
from common.utils import common_assert, read_json, html_util
from parameterized import parameterized


class TestOpenAccount(unittest.TestCase):

    def setUp(self) -> None:
        # 创建session对象
        self.session = requests.session()
        # 实例化封装接口请求的类
        # 1.实例化注册登陆登陆的类
        self.login_api = RegLoginApi(self.session)
        # 2.实例化开户的类
        self.open_api = OpenAccountApi(self.session)

    def tearDown(self) -> None:
        # 关闭session对象
        self.session.close()

    # 实名认证测试用例
    @parameterized.expand(read_json("/open_account", "realnameApi"))
    def test01_realname(self, username, password, realname, card_id, exp_status_code, exp_code, description, check):
        # 实现登录接口
        body = {"keywords": username, "password": password}
        self.login_api.user_login(body)
        # 实现实名认证接口
        resp = self.open_api.realname(realname, card_id)
        # 断言
        # 基础断言
        common_assert(self, resp, exp_status_code, exp_code, description)
        # 用户信息断言
        if check == 0:
            dict_data = resp.json()
            self.assertEqual("110****010", dict_data.get('data').get('card_id'))
            self.assertEqual("张**", dict_data.get('data').get('realname'))

    # 开户测试用例
    def test02_open_account(self):
        # 实现登录接口
        body = {"keywords": "13389898980", "password": "123456"}
        self.login_api.user_login(body)
        # 实现开户接口
        resp = self.open_api.open_account()
        # 断言
        common_assert(self, resp, 200, 200, 'form')
        # dict_data = resp.json()
        # self.assertEqual(200, resp.status_code)
        # self.assertEqual(200, dict_data.get('status'))
        # self.assertIn('action', dict_data.get('description').get('form'))

    # 第三方开户测试用例
    def test03_third_open_account(self):
        # 完成开户接口，获取response对象
        body = {"keywords": "13389898980", "password": "123456"}
        self.login_api.user_login(body)
        resp_open_account = self.open_api.open_account()

        # 调用封装获取第三方接口请求数据的方法，获取第三方开户接口的url
        url = html_util(resp_open_account)[0]
        print(f'url = {url}')
        # 调用封装获取第三方接口请求数据的方法，获取第三方开户接口的请求体数据
        form_dict = html_util(resp_open_account)[1]
        print(f'url = {form_dict}')
        # 调用封装的第三方开户接口的方法，获取第三方开户接口返回的响应结果
        resp_3 = self.open_api.third_open_account(url, form_dict)
        # 断言
        self.assertEqual(200, resp_3.status_code)
        self.assertIn("OK", resp_3.text)
